package com.example.lishop.repository

import com.example.lishop.entity.BCompanyEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * Created on 2021/3/14 18:00
 * @author shuguo
 */
interface CompanyRepository : JpaRepository<BCompanyEntity, Int>{

    fun findByCompanyId(companyId: String): BCompanyEntity?

    @Transactional(timeout = 10)
    @Modifying
    @Query("update BCompanyEntity set sts = '1' where companyId = ?1")
    fun del(companyId: String)

    @Transactional(timeout = 10)
    @Modifying
    @Query("update BCompanyEntity bean set " +
            "bean.name = CASE WHEN :#{#item.name} IS NULL THEN bean.name ELSE :#{#item.name} END " +
            ", bean.descript = CASE WHEN :#{#item.descript} IS NULL THEN bean.descript ELSE :#{#item.descript} END ," +
            "bean.type = CASE WHEN :#{#item.type} IS NULL THEN bean.type ELSE :#{#item.type} END ," +
            "bean.tag = CASE WHEN :#{#item.tag} IS NULL THEN bean.tag ELSE :#{#item.tag} END ," +
            "bean.status = CASE WHEN :#{#item.status} IS NULL THEN bean.status ELSE :#{#item.status} END ," +
            "bean.sts = CASE WHEN :#{#item.sts} IS NULL THEN bean.sts ELSE :#{#item.sts} END " +
            "where bean.companyId = :#{#item.companyId}")
    fun update(item:BCompanyEntity)
}